package com.dd.cloud.user.service.chknowl.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.chknowl.ChknowlReadCount;
import com.dd.cloud.user.mapper.chknowl.ChknowlReadCountMapper;
import com.dd.cloud.user.service.chknowl.IChknowlReadCountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author Lx
 * @version 1.0
 * @date 2024/1/4
 */
@Slf4j
@Service
public class ChknowlReadCountServiceImpl extends ServiceImpl<ChknowlReadCountMapper, ChknowlReadCount> implements IChknowlReadCountService {
    /**
     * 添加书院阅读数量记录
     *
     * @param id    id根据类型判断
     * @param type  类型 1课程 2书籍 3中药 4方剂 5医案
     * @param docId 医生id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addChknowlReadCount(Integer id, Integer type, Integer docId) {
        Integer userId;
        Integer userType;
        if (docId != null) {
            userId = docId;
            userType = LoginInfo.UserType.DOCTOR;
        } else {
            LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
            if (loginInfo == null) {
                throw new ZekeException(400, "医生id不能为空");
            }
            userId = loginInfo.getId();
            userType = loginInfo.getType();
        }
        QueryWrapper<ChknowlReadCount> qw = new QueryWrapper<>();
        qw.lambda().eq(ChknowlReadCount::getUserId, userId);
        qw.lambda().eq(ChknowlReadCount::getUserType, userType);
        qw.lambda().eq(ChknowlReadCount::getModelId, id);
        qw.lambda().eq(ChknowlReadCount::getModelType, type);
        int count = count(qw);
        if (count == 0) {
            ChknowlReadCount chknowlReadCount = new ChknowlReadCount();
            chknowlReadCount.setUserId(userId);
            chknowlReadCount.setUserType(userType);
            chknowlReadCount.setModelId(id);
            chknowlReadCount.setModelType(type);
            chknowlReadCount.setCreateDate(LocalDateTime.now());
            save(chknowlReadCount);
        }
    }

    /**
     * 查询书院(课程/书籍/中药/方剂/医案)阅读数量
     *
     * @param id    id根据类型判断
     * @param type  类型 1课程 2书籍 3中药 4方剂 5医案
     * @param docId 医生id
     */
    @Override
    public Integer getChknowlReadCountTotal(Integer id, Integer type, Integer docId) {
//        Integer userId;
//        Integer userType;
//        if (docId != null) {
//            userId = docId;
//            userType = LoginInfo.UserType.DOCTOR;
//        } else {
//            LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
//            userId = loginInfo.getId();
//            userType = loginInfo.getType();
//        }
        QueryWrapper<ChknowlReadCount> qw = new QueryWrapper<>();
//        qw.lambda().eq(ChknowlReadCount::getUserId, userId);
//        qw.lambda().eq(ChknowlReadCount::getUserType, userType);
        qw.lambda().eq(ChknowlReadCount::getModelId, id);
        qw.lambda().eq(ChknowlReadCount::getModelType, type);
        return count(qw);
    }
}
